package com.hsnn.medstgmini.subscribe.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.subscribe.model.BidSubscribeDetail;
import com.hsnn.medstgmini.subscribe.model.BidSubscribeRecord;
import com.hsnn.medstgmini.subscribe.service.BidSubscribeRecordManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 预约记录控制器
 * @author 韩守松
 * @date 2015年9月1日
 */
@Controller
@RequestMapping("/bidSubscribeRecord")
public class BidSubscribeRecordController {
	private static final Logger log = Logger.getLogger(BidSubscribeRecordController.class);
	
	@Autowired
	private BidSubscribeRecordManager bidSubscribeRecordManager;
	
	@Autowired
	private HttpServletRequest request;
	
	/**
	 * @category 跳转到生产企业预约记录页面
	 * @author 韩守松
	 * @date   2015年9月1日
	 * @param  @return
	 */
	@RequestMapping("/toRecordListJsp")
	public String toRecordListJsp(){
		return "/subscribe/produce/recordList";
	}
	
	/**
	 * @category 获取生产企业预约记录信息 
	 * @author 韩守松
	 * @date   2015年9月1日
	 * @param  @param page
	 * @param  @param bidSubscribeDetail
	 * @param  @return
	 */
	@RequestMapping("/getRecordData")
	@ResponseBody
	public Pagination getRecordData(Pagination page,BidSubscribeDetail bidSubscribeDetail){
		try{
		String subscribeDate = request.getParameter("subscribeDate");
		page = new Pagination(request);
		BidSubscribeRecord bidSubscribeRecord = new BidSubscribeRecord();
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		bidSubscribeRecord.setCompId(user.getId());
		if(StringUtils.isNotBlank(subscribeDate)){
			bidSubscribeDetail.setSubscribeDate(new SimpleDateFormat("yyyy-MM-dd").parse(subscribeDate));
		}
		page=bidSubscribeRecordManager.getSubscribeRecordList(page,bidSubscribeDetail,bidSubscribeRecord);
		page.setSuccess(true);
		}catch(Exception e){
			log.error("获取企业预约记录信息出错"+e);
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * @category 跳转到预约成功后的页面
	 * @author 韩守松
	 * @date   2015年9月1日
	 * @param  @return
	 */
	@RequestMapping("/toSuccessJsp")
	public String toSuccessJsp(){
		request.setAttribute("subscribeDate", request.getParameter("subscribeDate"));
		request.setAttribute("subscribeStarttime", request.getParameter("subscribeStarttime"));
		request.setAttribute("subscribeEndtime", request.getParameter("subscribeEndtime"));
		request.setAttribute("projName", request.getParameter("projName"));
		return "/subscribe/produce/success";
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("/toListSuccessJsp")
	public String toListSuccessJsp(String bidSubscribeId){
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		String compId = user.getId();
		List list = bidSubscribeRecordManager.getSuccessData(compId,bidSubscribeId);
		if(list.size() > 0){
			request.setAttribute("list", list.get(0));
		}else{
			request.setAttribute("msg", "您还没有进行预约");
		}
		return "/subscribe/produce/listSuccess";
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public  Pagination updateRecord(Pagination page,BidSubscribeRecord bidSubscribeRecord){
		try{
			SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
			if(StringUtils.isNotBlank(bidSubscribeRecord.getBidSubscribeRecordId())){
				bidSubscribeRecord.setUpdUser(user.getId());
				bidSubscribeRecord.setUpdDatetime(new Date());
				bidSubscribeRecord.setProcessDatetime(new Date());
				bidSubscribeRecordManager.update(bidSubscribeRecord);
				page.setSuccess(true);
			}
		}catch(Exception e){
			log.error("更新预约记录信息错误");
			page.setSuccess(false);
		}
		return page;
	}
}
